package com.yang.crawler.article.executor;

import com.yang.crawler.DownloadArticleCommand;
import com.yang.crawler.dto.data.DownloadDTO;
import com.yang.crawler.html.parser.HtmlBlockParser;
import com.yang.crawler.pdf.Html2PdfBuilder;
import com.yang.crawler.pdf.request.Html2PdfBuildRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class DownloadArticleCmdExecutor {
    @Resource
    private Html2PdfBuilder html2PdfBuilder;
    public DownloadDTO execute(DownloadArticleCommand downloadArticleCommand) {
        String title = downloadArticleCommand.getTitle();
        String htmlContent = downloadArticleCommand.getHtmlContent();

        Html2PdfBuildRequest html2PdfBuildRequest = new Html2PdfBuildRequest();
        html2PdfBuildRequest.setTitle(title);
        html2PdfBuildRequest.setHtmlBlockDTOList(HtmlBlockParser.parseHtml(htmlContent));
        byte[] data = html2PdfBuilder.html2Pdf(html2PdfBuildRequest);

        DownloadDTO downloadDTO = new DownloadDTO();
        downloadDTO.setData(data);
        downloadDTO.setFilename(title + ".pdf");
        return downloadDTO;
    }
}
